package com.imay.live.adapter;

import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.imay.live.model.AreaModel;
import com.imay.live.R;
import com.timehop.stickyheadersrecyclerview.StickyRecyclerHeadersAdapter;

import java.util.List;

/**
 * Created by Administrator on 2017/3/29.
 */

public class AreaAdapter extends BaseQuickAdapter<AreaModel, BaseViewHolder> implements StickyRecyclerHeadersAdapter<BaseViewHolder> {

    public AreaAdapter(List<AreaModel> list) {
        super(R.layout.item_local_area, list);
    }

    @Override
    protected void convert(BaseViewHolder helper, AreaModel item) {
        if (item.getCode() == null) {
            helper.setVisible(R.id.tv_area_tag, true);
            helper.setVisible(R.id.tv_area_country, false);
            helper.setVisible(R.id.tv_area_country_code, false);

            helper.setText(R.id.tv_area_tag, item.getArea());
            RelativeLayout rlLocal = helper.getView(R.id.item_local_item);
            rlLocal.getLayoutParams().height = (int) helper.getConvertView().getResources().getDimension(R.dimen.size_0dp);
            rlLocal.setBackgroundColor(helper.getConvertView().getResources().getColor(R.color.bg_e5));
            helper.setVisible(R.id.item_local_item, true);
        } else {
            helper.setVisible(R.id.item_local_item, true);
            helper.setVisible(R.id.tv_area_tag, false);
            helper.setVisible(R.id.tv_area_country, true);
            helper.setVisible(R.id.tv_area_country_code, true);

            helper.setText(R.id.tv_area_country, item.getArea());
            helper.setText(R.id.tv_area_country_code, "+" + item.getCode());

            RelativeLayout rlLocal = helper.getView(R.id.item_local_item);
            rlLocal.getLayoutParams().height = (int) helper.getConvertView().getResources().getDimension(R.dimen.user_local_item_label);
            rlLocal.setBackgroundColor(Color.WHITE);
        }
    }

    @Override
    public long getHeaderId(int position) {
        return getItem(position).getAscription().charAt(0);
    }

    @Override
    public BaseViewHolder onCreateHeaderViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_local_area, parent, false);
        return new BaseViewHolder(view);
    }

    @Override
    public void onBindHeaderViewHolder(BaseViewHolder helper, int position) {
        helper.setVisible(R.id.tv_area_tag, true);
        helper.setVisible(R.id.tv_area_country, false);
        helper.setVisible(R.id.tv_area_country_code, false);

        helper.setText(R.id.tv_area_tag, getItem(position).getAscription());
        RelativeLayout rlLocal = helper.getView(R.id.item_local_item);
        rlLocal.getLayoutParams().height = (int) helper.getConvertView().getResources().getDimension(R.dimen.user_local_item_a);
        rlLocal.setBackgroundColor(helper.getConvertView().getResources().getColor(R.color.bg_f5));
    }
}
